package triggerhelper.controllers;

import java.awt.Dimension;
import javax.swing.*;
import triggerhelper.TextFrame;
import triggerhelper.dataHolders.Trigger;
import triggerhelper.errorChecks.ErrorCheck;
import triggerhelper.errorChecks.possibleErrors.MinMax;
import triggerhelper.helperClasses.TriggerCompiler;
import triggerhelper.tabs.FinalTab;
import triggerhelper.triggerCreator;

public class FinalController {
    
    private FinalTab aTab;
    private triggerCreator parent;
    private JTextField fIterations;
    private JButton bHelp, bSubmit;
    private JComboBox cbOptions;
    private Trigger currentTrigger;
    private ErrorCheck eIterations;

    public FinalController(FinalTab aTab_, triggerCreator parent_){
        aTab = aTab_;
        parent = parent_;
        setEasyAccessFields();
        setListeners();

        eIterations = new ErrorCheck("Iterations value", aTab);
        eIterations.addError(new MinMax(0, 50000));
    }


    private void setEasyAccessFields() {
        fIterations = aTab.getfIterations();
        bHelp = aTab.getbHelp();
        bSubmit = aTab.getbSubmit();
        cbOptions = aTab.getCbOptions();
        currentTrigger = null;
    }

    private void setListeners() {
        aTab.addComponentListener(new java.awt.event.ComponentAdapter() {
            @Override
            public void componentShown(java.awt.event.ComponentEvent evt) {
                loadPane();
            }
            @Override
            public void componentHidden(java.awt.event.ComponentEvent evt) {
                saveIterations();
            }
        });
        fIterations.addFocusListener(new java.awt.event.FocusAdapter() {
            @Override
            public void focusGained(java.awt.event.FocusEvent evt) {
                fIterations.selectAll();
            }
        });
        bHelp.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                help();
            }
        });
        bSubmit.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                submitOption();
            }
        });
    }

    public void loadPane() {
        currentTrigger = (Trigger)parent.getCurrentTrigger();
        fIterations.setText(""+currentTrigger.getNumIterations());
    }

    private void saveIterations(){
        if(eIterations.hasNoErrors(fIterations.getText())){
            int numIterations = Integer.parseInt(fIterations.getText());
            currentTrigger.setNumIterations(numIterations);
        }
    }
    
    private void help(){

    }
    private void submitOption(){
        int option = cbOptions.getSelectedIndex();
        saveIterations();
        switch(option){
            case 0:
                TextFrame display = new TextFrame();
                display.setTitle("Trigger Output");
                display.setlMainTitle("Trigger Text");
                display.setPreferredSize(new Dimension(462, 359));
                int x = parent.getX() + parent.getWidth();
                int y = parent.getY();
                display.setLocation(x, y);
                display.setVisible(true);
                TriggerCompiler.compileTrigger(display, currentTrigger);
                display.gettOutput().setCaretPosition(0);
                break;
            case 1:
                TriggerCompiler.compileProject(parent, parent.getCurrentProject());
                break;
        }
    }
}
